﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace Space_Conquest
{
    class MainMenu : IMenu 
    {
        
        #region Member Variables
        /// <summary>
        /// creates Variable for menu
        /// </summary>
        
        InputManager _inputManager = new InputManager();
        InputManager _inputManagerMainMenu = new InputManager();
        InputManager _inputManagerStart = new InputManager();
        InputManager _inputManagerOption = new InputManager();
        InputManager _inputManagerCredit = new InputManager();

        bool mainMenuVisible = true;
        bool singlePlayerSelected = false;
        bool creditSelected = false;
        bool optionSelected = false;
        bool multiplayerSelected = false;

        List<String> mapfiles = new List<string>(Directory.GetFiles(Globals.RESOURCE_DIRECTORY + "Maps"));
        int i = 0;
        string mapChoosen = "";
        static List<String> musicfiles = new List<string>(Directory.GetFiles(Globals.RESOURCE_DIRECTORY + "Music/Intro"));
        static int j = 0;

        #region Main Menu
            Texture titleTexure = new Texture("Images/Title.bmp");
            Sprite titleSprite = new Sprite(((Globals.iScreenWidth /2) - 325), 30, 650, 200);
            MenuButton singlePlayer = new MenuButton(((Globals.iScreenWidth/2)- 150), 225, 250, 75, "Images/SinglePlayer.bmp");
            MenuButton multiplayer = new MenuButton(((Globals.iScreenWidth / 2) - 150), 325, 250, 75, "Images/Multiplayer.bmp");
            MenuButton option = new MenuButton(((Globals.iScreenWidth / 2) - 150), 425, 250, 75, "Images/OptionButton.bmp");
            MenuButton credit = new MenuButton(((Globals.iScreenWidth / 2) - 150), 525, 250, 75, "Images/CreditsButton.bmp");
            MenuButton exit = new MenuButton(((Globals.iScreenWidth / 2) - 150), 625, 250, 75, "Images/ExitButton.bmp");
        #endregion

        #region Start Menu
            MenuButton _mapArrowUp = new MenuButton((Globals.iScreenWidth / 2) + 80, 350, 24, 17, "Images/UpArrow.bmp");
            MenuButton _mapArrowDown = new MenuButton((Globals.iScreenWidth / 2) + 80, 368, 24, 17, "Images/DownArrow.bmp");
            MenuButton _mapButton = new MenuButton((Globals.iScreenWidth/2) - 130, 355, 200, 34, "Images/OnScreenDisplayButton.bmp");
            MenuButton _start = new MenuButton(((Globals.iScreenWidth /2) - 75) , 550, 150, 50, "Images/StartButton.bmp");
            TextBox _playerName = new TextBox((Globals.iScreenWidth /2) - 240, 250);
            TextBox _ipAddress = new TextBox((Globals.iScreenWidth / 2) - 240, 455);
        #endregion

        # region Option Menu
            MenuButton _optionMusic = new MenuButton(((Globals.iScreenWidth / 2) - 190), 200, 190, 190, "Images/Music.bmp");
            MenuButton _optionMusicOn = new MenuButton((Globals.iScreenWidth / 2), 200, 190, 190, "Images/On.bmp");
            MenuButton _optionMusicOff = new MenuButton(((Globals.iScreenWidth / 2) + 150), 200, 190, 190, "Images/Off.bmp");
            MenuButton _optionSound = new MenuButton(((Globals.iScreenWidth / 2) - 190 ), 300, 190, 190, "Images/Sound.bmp");
            MenuButton _optionSoundOn = new MenuButton((Globals.iScreenWidth / 2), 300, 190, 190, "Images/On.bmp");
            MenuButton _optionSoundOff = new MenuButton(((Globals.iScreenWidth / 2+ 150)), 300, 190, 190, "Images/Off.bmp");
            MenuButton _optionSoundVolume = new MenuButton(((Globals.iScreenWidth / 2) - 190), 400, 190, 190, "Images/Volume.bmp");
            MenuButton _optionSoundDown = new MenuButton((Globals.iScreenWidth / 2), 400, 190, 190, "Images/Down.bmp");
            MenuButton _optionSoundUp = new MenuButton(((Globals.iScreenWidth / 2) + 150 ), 400, 190, 190, "Images/Up.bmp");
            MenuButton _optionSoundBack = new MenuButton(800, 700, 200, 50, "Images/BackButton.bmp");
        #endregion

        #region Credit Screen
            MenuButton _creditBack = new MenuButton(800, 700, 200, 50, "Images/BackButton.bmp");
            MenuButton credits = new MenuButton(0, 0, Globals.iScreenWidth, Globals.iScreenHeight, "Images/Credits.bmp");
        #endregion

        #endregion

        #region Constructor

            public MainMenu()
            {

                _inputManagerMainMenu.Add(singlePlayer);
                _inputManagerMainMenu.Add(multiplayer);
                _inputManagerMainMenu.Add(option);
                _inputManagerMainMenu.Add(credit);
                _inputManagerMainMenu.Add(exit);
                _inputManagerStart.Add(_mapArrowUp);
                _inputManagerStart.Add(_mapArrowDown);
                _inputManagerStart.Add(_start);
                _inputManagerOption.Add(_optionMusicOn);
                _inputManagerOption.Add(_optionMusicOff);
                _inputManagerOption.Add(_optionSoundOn);
                _inputManagerOption.Add(_optionSoundOff);
                _inputManagerOption.Add(_optionSoundBack);
                _inputManagerCredit.Add(_creditBack);

                singlePlayer.OnButtonClick += new MenuButtonHandler(singlePlayer_OnButtonClick);
                multiplayer.OnButtonClick += new MenuButtonHandler(multiplayer_OnButtonClick);
                option.OnButtonClick += new MenuButtonHandler(option_OnButtonClick);
                credit.OnButtonClick += new MenuButtonHandler(credit_OnButtonClick);
                exit.OnButtonClick += new MenuButtonHandler(exit_OnButtonClick);
                _mapArrowUp.OnButtonClick += new MenuButtonHandler(_mapArrowUp_OnButtonClick);
                _mapArrowDown.OnButtonClick += new MenuButtonHandler(_mapArrowDown_OnButtonClick);
                _start.OnButtonClick += new MenuButtonHandler(_start_OnButtonClick);
                _optionMusicOn.OnButtonClick += new MenuButtonHandler(_optionMusicOn_OnButtonClick);
                _optionMusicOff.OnButtonClick += new MenuButtonHandler(_optionMusicOff_OnButtonClick);
                _optionSoundOn.OnButtonClick += new MenuButtonHandler(_optionSoundOn_OnButtonClick);
                _optionSoundOff.OnButtonClick += new MenuButtonHandler(_optionSoundOff_OnButtonClick);
                _optionSoundBack.OnButtonClick += new MenuButtonHandler(_optionSoundBack_OnButtonClick);
                _creditBack.OnButtonClick += new MenuButtonHandler(_creditBack_OnButtonClick);

                _playerName.OnEnterPressed += new TextBoxEnterHandler(_start_OnButtonClick);
                _ipAddress.OnEnterPressed += new TextBoxEnterHandler(_start_OnButtonClick);
                PlayIntroMusic();
            }
        #endregion

        public void PlayIntroMusic()
        {
            if (j == musicfiles.Count - 1)
            {
                j = 0;
            }
            else
            {
                Audio.PlayMusic("Music/Intro/" + Path.GetFileName(musicfiles[j]));
                j++;
            }
        }

        #region Events

        void singlePlayer_OnButtonClick()
        {
            mainMenuVisible = false;
            singlePlayerSelected = true;
        }

        void multiplayer_OnButtonClick()
        {
            mainMenuVisible = false;
            singlePlayerSelected = true;
            multiplayerSelected = true;
        }

        void option_OnButtonClick()
        {
            mainMenuVisible = false;
            optionSelected = true;
        }

        void credit_OnButtonClick()
        {
            mainMenuVisible = false;
            creditSelected = true;
        }

        void exit_OnButtonClick()
        {
            Globals.EndProgram();
        }

        void _mapArrowUp_OnButtonClick()
        {
            if (i == mapfiles.Count -1)
            {
                i = 0;
            }
            else
            {
                i++;
            }
        }

        void _mapArrowDown_OnButtonClick()
        {
            if (i == 0)
            {
                i = mapfiles.Count - 1;
            }
            else
            {
                i--;
            }
        }

        void _start_OnButtonClick()
        {

            //multiplayerSelected, mapChoosen

            mapChoosen = Path.GetFileName(mapfiles[i]);
            GameInitializer settings = new GameInitializer();

            
            if (multiplayerSelected)
            {

                settings.IsMultiplayer = true;
                settings.NumberOfPlayers = 1;

                if (_ipAddress.Text == "")
                {
                    // Server

                    settings.IsServer = true;
                    settings.PlayerNames.Add(_playerName.Text);

                }
                else
                {
                    // Client

                    settings.IsMultiplayer = true;
                    settings.PlayerNames.Add(_playerName.Text);
                    settings.IsServer = false;
                    settings.ServerIP = _ipAddress.Text;

                }

            }
            else if (singlePlayerSelected)
            {

                settings.IsMultiplayer = false;

                settings.NumberOfPlayers = 4;
                settings.PlayerNames.Add(_playerName.Text);
                settings.PlayerNames.Add("Player 2");
                settings.PlayerNames.Add("Player 3");
                settings.PlayerNames.Add("Player 4");

            }

            settings.MapFileName = Path.GetFileNameWithoutExtension(mapfiles[i]);

            Globals.GameStack.Push(new GameEngine(settings));

            singlePlayerSelected = false;
            multiplayerSelected = false;
            mainMenuVisible = true;

            i = 0;
        }

        void _optionMusicOn_OnButtonClick()
        {
            PlayIntroMusic();
        }

        void _optionMusicOff_OnButtonClick()
        {
            Audio.StopMusic();
        }

        void _optionSoundOn_OnButtonClick()
        {
            
        }

        void _optionSoundOff_OnButtonClick()
        {
            
        }

        void _optionSoundBack_OnButtonClick()
        {
            optionSelected = false;
            mainMenuVisible = true;
        }

        void _creditBack_OnButtonClick()
        {
            creditSelected = false;
            mainMenuVisible = true;
        }

        #endregion

        #region Render
        /// <summary>
        /// Renders background and calls GameButton Render
        /// </summary>
        public void Render()
        {
            titleSprite.Render(titleTexure);

            if (mainMenuVisible)
            {
                singlePlayer.Render();
                multiplayer.Render();
                option.Render();
                credit.Render();
                exit.Render();
            }
            if (singlePlayerSelected)
            {
                Text.Render("Enter player name below.", Color.White, (Globals.iScreenWidth / 2) - 130, 220);
                _playerName.Render();
                Text.Render("Please choose a map from the list below.", Color.White, (Globals.iScreenWidth / 2) - 180, 320);
                _mapArrowUp.Render();
                _mapArrowDown.Render();
                _mapButton.Render();
                Text.Render(Path.GetFileName(mapfiles[i]), Color.White, (Globals.iScreenWidth / 2) - 120, 360);
                if (multiplayerSelected)
                {
                    Text.Render("Type IP Address below to become a client.", Color.White, (Globals.iScreenWidth / 2) - 200, 420);
                    _ipAddress.Render();
                }
                _start.Render();
            }
            if (creditSelected)
            {
                credits.Render();
                _creditBack.Render();
            }
            if (optionSelected)
            {
                _optionMusic.Render();
                _optionMusicOn.Render();
                _optionMusicOff.Render();
                _optionSound.Render();
                _optionSoundOn.Render();
                _optionSoundOff.Render();
                _optionSoundVolume.Render();
                _optionSoundUp.Render();
                _optionSoundDown.Render();
                _optionSoundBack.Render();
            }

        }
        #endregion

        #region IMenu Members

        public void UnloadSelf()
        {

            singlePlayer = null;
            option = null;
            credit = null;

            Globals.GameStack.Pop();
        }

        #endregion

        #region IMotion Members

        public void Motion()
        {
            
        }

        #endregion

        #region IInput Members

        public bool MouseDown(System.Windows.Forms.MouseButtons button, int x, int y)
        {
            if (mainMenuVisible)
            {
                _inputManagerMainMenu.MouseDown(button, x, y);
                return true;
            }
            else if (singlePlayerSelected)
            {
                _playerName.MouseDown(button, x, y);
                if (multiplayerSelected)
                    _ipAddress.MouseDown(button, x, y);
                _inputManagerStart.MouseDown(button, x, y);
            }
            else if (optionSelected)
            {
                _inputManagerOption.MouseDown(button, x, y);
                return true;
            }
            else if (creditSelected)
            {
                _inputManagerCredit.MouseDown(button, x, y);
                return true;
            }
            return _inputManager.MouseDown(button, x, y);
        }

        public bool MouseUp(System.Windows.Forms.MouseButtons button, int x, int y)
        {
            return _inputManager.MouseUp(button,x,y);
        }

        public bool MouseMove(int x, int y)
        {
            return _inputManager.MouseMove(x,y);
        }

        public bool KeyDown(System.Windows.Forms.Keys key, bool shift)
        {
            if (singlePlayerSelected)
            {
                _playerName.KeyDown(key, shift);
                if (multiplayerSelected)
                    _ipAddress.KeyDown(key, shift);
            }
            return _inputManager.KeyDown(key,shift);
        }

        public bool KeyUp(System.Windows.Forms.Keys key, bool shift)
        {
            return _inputManager.KeyUp(key, shift);
        }

        #endregion

    }
}
